package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import misc.Role;
import kontroler.Kontroler;

@SuppressWarnings("serial")
public class PlayerDialog extends JDialog{

	private JPanel panel = null;
	private JLabel clubLabel = null;
	private JLabel club = null;
	private JLabel countryLabel = null;
	private JLabel country = null;
	private JLabel nameLabel = null;
	private JTextField name = null;
	private JLabel lastNameLabel = null;
	private JTextField lastName = null;
	private JLabel yearOfBirthLabel = null;
	private JTextField yearOfBirth = null;
	private JLabel roleLabel = null;
	private JComboBox role = null;
	private JButton okButton = null;
	private JButton cancelButon = null;
	private SouthPanel southPanel = null;
	private Kontroler kontroler = null;
	
	public PlayerDialog(SouthPanel southPanel) {
		super();
		this.setLayout(new BorderLayout());
		this.southPanel = southPanel;
		this.kontroler = southPanel.getKontroler();
		initialize();
	}
	
	private void initialize(){
		this.add(getPanel(),BorderLayout.CENTER);
		this.setSize(new Dimension(220,280));
	}
	
	private JPanel getPanel(){
		if (panel == null){
			panel = new JPanel(new FlowLayout());
			panel.add(getClubLabel());
			panel.add(getClub());
			panel.add(getCountryLabel());
			panel.add(getCountry());
			panel.add(getNameLabel());
			panel.add(getNameField());
			panel.add(getLastNameLabel());
			panel.add(getLastName());
			panel.add(getYearOfBirthLabel());
			panel.add(getYearOfBirth());
			panel.add(getRoleLabel());
			panel.add(getRole());
			panel.add(getOkButton());
			panel.add(getCancelButton());
		}
		return panel;
	}

	private JLabel getClubLabel() {
		if (clubLabel == null){
			clubLabel = new JLabel("Club: ");
			clubLabel.setPreferredSize(new Dimension(70,20));
		}
		return clubLabel;
	}

	private JLabel getClub() {
		if (club == null){
			club = new JLabel(southPanel.getCenterPanel().getSelectedClub().getName());
			club.setPreferredSize(new Dimension(100,20));
		}
		return club;
	}

	private JLabel getCountryLabel() {
		if (countryLabel == null){
			countryLabel = new JLabel("Country");
			countryLabel.setPreferredSize(new Dimension(70,20));
		}
		return countryLabel;
	}

	private JLabel getCountry() {
		if (country == null){
			country = new JLabel(southPanel.getCenterPanel().getWestPanel().getCompSplitPane().getSelectedCountryValue());
			country.setPreferredSize(new Dimension(100,20));
		}
		return country;
	}

	private JLabel getNameLabel() {
		if (nameLabel == null){
			nameLabel = new JLabel("Name");
			nameLabel.setPreferredSize(new Dimension(70,20));
		}
		return nameLabel;
	}

	private JTextField getNameField() {
		if (name == null){
			name = new JTextField();
			name.setPreferredSize(new Dimension(100,20));
		}
		return name;
	}

	private JLabel getLastNameLabel() {
		if (lastNameLabel == null){
			lastNameLabel = new JLabel("Last name");
			lastNameLabel.setPreferredSize(new Dimension(70,20));
		}
		return lastNameLabel;
	}

	private JTextField getLastName() {
		if (lastName == null){
			lastName = new JTextField();
			lastName.setPreferredSize(new Dimension(100,20));
		}
		return lastName;
	}

	private JLabel getYearOfBirthLabel() {
		if (yearOfBirthLabel == null){
			yearOfBirthLabel = new JLabel("Year of Birth");
			yearOfBirthLabel.setPreferredSize(new Dimension(70,20));
		}
		return yearOfBirthLabel;
	}

	private JTextField getYearOfBirth() {
		if (yearOfBirth == null){
			yearOfBirth = new JTextField();
			yearOfBirth.setPreferredSize(new Dimension(100,20));
		}
		return yearOfBirth;
	}

	private JLabel getRoleLabel() {
		if (roleLabel == null){
			roleLabel = new JLabel("Role");
			roleLabel.setPreferredSize(new Dimension(70,20));
		}
		return roleLabel;
	}

	private JComboBox getRole() {
		if (role == null){
			role = new JComboBox(getValues());
			role.setPreferredSize(new Dimension(100,20));
		}
		return role;
	}
	
	private JButton getOkButton(){
		if (okButton == null){
			okButton = new JButton("OK");
			okButton.setPreferredSize(new Dimension(70,20));
			okButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					kontroler.addPlayer(getNameField().getText(), getLastName().getText(), getYearOfBirth().getText(), Role.valueOf((String)getRole().getSelectedItem()), southPanel.getCenterPanel().getSelectedClub(), southPanel.getCenterPanel().getWestPanel().getCompSplitPane().getSelectedCountry());				
					setVisible(false);
					southPanel.populateTable();
				}
			});
		}
		return okButton;
	}
	
	private JButton getCancelButton(){
		if (cancelButon == null){
			cancelButon = new JButton("Cancel");
			cancelButon.setPreferredSize(new Dimension(70,20));
			cancelButon.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					setVisible(false);
					
				}
			});
		}
		return cancelButon;
	}
	
	private String[] getValues(){
		Role[] roles = Role.values();
		String [] roleValues = new String[Role.values().length];
		for(int i = 0; i < roles.length; i++){
			roleValues[i] = roles[i].toString();
		}
		return roleValues;
	}
	
}
